package com.training.java.module2;


public class AskQuestion implements UserAction  {

    private int numberOfQuestion;

    private boolean isCorrect;

    private TestEvaluation testEvaluation;

    private AskQuestion(){
        numberOfQuestion = 0;
        testEvaluation = new TestEvaluation();
    }
    private static class AskQuestionHolder {
        public static final AskQuestion INSTANCE =  new AskQuestion();
    }

    public static AskQuestion getInstance(){
        return  AskQuestionHolder.INSTANCE;
    }
    private void  askQuestion(){
        if(numberOfQuestion < QuestionsCollection.getInstance().getQuestionsCollectionSize()){
            printQuestionTitle();
            isCustomerAnswerCorrect();
            testEvaluation.addPointForCorrectAnswer(isCorrect);
            testEvaluation.printNumberOfCorrectAnswers();
        } else {
            System.out.println("questions is over");
        }
    }
    private void printQuestionTitle(){
        QuestionsCollection.getInstance().printQuestionTitle(numberOfQuestion);
    }
    private void isCustomerAnswerCorrect(){
        isCorrect = QuestionsCollection.getInstance().isCorrectAnswer(numberOfQuestion);
        numberOfQuestion++;
    }
    public void userAction(){
        askQuestion();
    }
    /**
     * Prepare questions to asking another user
     */
    public void prepareForAnotherUser(){
        numberOfQuestion = 0;
        QuestionsCollection.getInstance().shuffleQuestions();
        testEvaluation = new TestEvaluation();
    }
    public boolean getIsCorrect(){
        return isCorrect;
    }
}
